<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/common/taglib.jsp" %>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script>
	//# sourceURL=orgPersonList.js
	
	var isDisplayChild = false;
	var isOnlyMainPosition = false;
	
	$(document).ready(function() {
		$("#ckDisplayChild").change(function() {
			isDisplayChild = $("#ckDisplayChild").is(':checked');
			mtr_pageIndex = 1;
			loadPersonGrid(person_org_treeNodeId);
		});
		$("#ckOnlyMainPosition").change(function() {
			isOnlyMainPosition = $("#ckOnlyMainPosition").is(':checked');
			mtr_pageIndex = 1;
			loadPersonGrid(person_org_treeNodeId);
		});		
	});

	function addPerson() {
		if (check_addPerson() == false) return;

		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/person/add.action?orgId=' + person_org_treeNodeId
		});
	}

	function editPerson() {
		if (checkSelected() == false) return;
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/person/modify.action?personId=' + selPersonId
		});		
	}

	function changeState(state) {
		if (checkSelected() == false) return;
		
		var preWord = '非主岗人员也将被相应处理,';
		if (selPerson_isMainPos == false) {
			preWord = '';
		}
		
		if(state == "Delete" && confirm(preWord + '确定要删除"' + selPersonName + '"吗？') == false) {
			return;
		}
		if(state == "Invalid" && confirm(preWord + '确定要禁用"' + selPersonName + '"吗？') == false) {
			return;
		}		
		
		var url = "${ctx}/org/person/changeState.action?personId=" + 
				selPersonId + "&validState=" + state + "&orgId=" + selRelateOrgId;
		$.get(url, function(data) {	    				    		
			if(data == "success") {
				loadPersonGrid(person_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});			
	}
	
	function resetPassword() {
		if (checkSelected() == false) return;
		
		if(confirm('确定要重置"' + selPersonName + '"密码？') == false) {
			return;
		}		
		
		var url = "${ctx}/org/person/resetPassword.action?personId=" + selPersonId;
		$.get(url, function(data) {
			if(data == "success") {
				alert("密码重置成功");
				loadPersonGrid(person_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});	
	}
	
	function positionView() {
		if (checkSelected() == false) return;
		
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/person/selectPosition.action?personId=' + selPersonId
		});		
	}
	
	function onSelectPositionEvent(orgId) {
		$('#dialog_main').modal('hide');
				
		setMainPosition(selPersonId, orgId);
	}	
	
	function setMainPosition(personId, orgId) {
		
		var url = "${ctx}/org/person/setMainPosition.action?personId=" + personId + 
		"&orgId=" + orgId;

		$.get(url, function(data) {
			if(data == "success") {
				loadPersonGrid(person_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});			
	}
		
	var treeView_cmd = null;
	function treeView(cmd) {
		if (checkSelected() == false) return;	
		treeView_cmd = cmd;
		
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/organization/treeView.action?orgId=0&isContainSelf=false'
		});		
	}
	
	function onSelectOrgEvent(orgId) {
		$('#dialog_main').modal('hide');
		
		if (treeView_cmd == "changeOrg") {
			changeOrg(orgId);
		} else if (treeView_cmd == "joinMember") {
			joinMember(orgId);
		}		
	}	
	
	function changeOrg(orgId) {
		var url = "${ctx}/org/person/changeOrg.action?personId=" + selPersonId + 
		"&newOrgId=" + orgId + "&oldOrgId=" + person_org_treeNodeId;

		$.get(url, function(data) {
			if(data == "success") {
				loadPersonGrid(person_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});	
	}		
	
	function joinMember(orgId) {
		var url = "${ctx}/org/person/joinMember.action?personId=" + selPersonId + "&orgId=" + orgId;

		$.get(url, function(data) {
			if(data == "success") {
				loadPersonGrid(person_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});	
	}	

	function checkSelected() {
		if (selPersonId == null) {
			alert("请选择表格行");
			return false;
		} else {
			return true;
		}
	}
	
	function check_addPerson() {		
		if (person_org_treeNodeId == null) {
			alert("请选择组织节点");
			return false;			
		} else if (person_org_treeNodeId == 0) {
			alert("根节点下，不能新建人员");
			return false;						
		}		
		return true;
	}

	function dialogSubmitSuccess() {
		$('#dialog_main').modal('hide');
		loadPersonGrid(person_org_treeNodeId);
	}

    function refreshPage() {
		var url = "/org/person/manage.action";
		load_main_content(url);
    }
    
    function loadPersonGrid(orgId) {
    	if (orgId == null) return;
    	var searchName = $("#inputSearchName").val();
    	
    	var url = "/org/person/grid.action?orgId=" + orgId + 
    			"&isDisplayChild=" + isDisplayChild + 
    			"&isOnlyMainPosition=" + isOnlyMainPosition + 
    			"&searchName=" + searchName +
    			"&pageIndex=" + mtr_pageIndex;
    	
		$.get("${ctx}" + url, function(data) {
			$("#gridPersonPage").empty().append(data);
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});    	
    }
    
    var mtr_pageIndex = 1;
	function onPageClick(n) {
		mtr_pageIndex = n;
		loadPersonGrid(person_org_treeNodeId);
	}        

</script>

</head>
<body>

<div>
	<h3>人员管理</h3>
</div>

<div class="col-md-2 column" style="border-right:1px solid #ccc;">
	<script>
		var person_org_setting = {
			check: {
				enable: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
	        callback: {
	            onClick: person_org_onTreeClick
	        }
		};
		
		var person_org_treeNodeId = null;
		var person_org_treeNodeName = null;
		
		function person_org_onTreeClick(event, treeId, treeNode, clickFlag) {
			person_org_treeNodeId = treeNode.id;
			person_org_treeNodeName = treeNode.name;
			
			//alert(person_org_treeNodeId + ":" + person_org_treeNodeName);
			mtr_pageIndex = 1;
			loadPersonGrid(person_org_treeNodeId);
		}
	
		var person_org_Nodes = ${listOrganization};
	
		$(document).ready(function(){
			$.fn.zTree.init($("#person_orgTreeView"), person_org_setting, person_org_Nodes);
		});
	</script>
	<div class="row clearfix">
	<ul id="person_orgTreeView" class="ztree"></ul>
	<br/>
	</div>
</div>

<div class="col-md-10 column">
	<nav class="navbar navbar-default" role="navigation">
	  <ul class="nav navbar-nav navbar-left">
	    <li>
	      <a href="javascript:addPerson()">新建</a></li>
	    <li>
	      <a href="javascript:editPerson()">编辑</a></li> 
	    <li>
	      <a href="javascript:treeView('joinMember')">分配</a></li>
	    <li>
	      <a href="javascript:treeView('changeOrg')">转到</a></li>
	      
	    <li class="dropdown">
	      <a href="#" class="dropdown-toggle" data-toggle="dropdown">选项
	        <b class="caret"></b></a>
	      <ul class="dropdown-menu">
	        <li>&nbsp;<input id="ckDisplayChild" type="checkbox">&nbsp;显示下级</li>
	        <li>&nbsp;<input id="ckOnlyMainPosition" type="checkbox">&nbsp;仅显示主岗</li>        
	      </ul>
	    </li>
	      	      	      	       
	    <li class="dropdown">
	      <a href="#" class="dropdown-toggle" data-toggle="dropdown">更多
	        <b class="caret"></b></a>
	      <ul class="dropdown-menu">
	        <li>
	          <a href="javascript:changeState('Invalid')">禁用</a></li>
	        <li>
	          <a href="javascript:changeState('Normal')">启用</a></li>
		    <li>
		      <a href="javascript:changeState('Delete')">删除</a></li>
	        <li class="divider"></li>
	        <li>
	          <a href="javascript:resetPassword()">重置密码</a></li>  
	        <li class="divider"></li>
	        <li>
	          <a href="javascript:positionView()">设置主岗</a></li>  	          
	      </ul>
	    </li>
	  </ul>
	  
      <form class="navbar-form navbar-right" role="search">
			<div class="form-group">
                <input id="inputSearchName" type="text" class="form-control" placeholder="Search">&nbsp;
            </div>
      </form>
	</nav>
	
	<div id="gridPersonPage" class="clearfix">
	<%@ include file="/WEB-INF/view/modules/org/personGrid.jsp" %>
	</div>
	
<!-- 	<div class="clearfix">
	  <div class="col-md-6 column" style="background-color:white;text-align: left;">当前显示1~20条,共161条</div>
	  <div class="col-md-6 column" style="background-color:white;text-align: right;">
	    <ul class="pagination" style="margin-top: 0px;margin-bottom: 10px;">
	      <li>
	        <a href="#">&laquo;</a></li>
	      <li class="active">
	        <a href="#">1</a></li>
	      <li>
	        <a href="#">2</a></li>
	      <li>
	        <a href="#">3</a></li>
	      <li>
	        <a href="#">4</a></li>
	      <li>
	        <a href="#">5</a></li>
	      <li>
	        <a href="#">..</a></li>
	      <li>
	        <a href="#">&raquo;</a></li>
	    </ul>
	  </div>
	</div> -->
	
</div>

</body>
</html>